package online.shiming.file.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

@Configuration
public class DruidConfig {

    @ConfigurationProperties(prefix = "spring.datasource")
    @Bean
    public DataSource druidDataSource() {
        return new DruidDataSource();
    }


    /**
     * 配置监控服务器
     *
     * @return 返回监控注册的servlet对象
     */
    @Bean
    public ServletRegistrationBean<?> statViewServletDemo() {
        ServletRegistrationBean<StatViewServlet> srb =
                new ServletRegistrationBean<>(new StatViewServlet(),
                        "/druid/*");
        // 添加IP白名单
        //srb.addInitParameter("allow", "0.0.0.0");
        //srb.addInitParameter("allow", "192.168.1.*");
        // 添加IP黑名单，当白名单和黑名单重复时，黑名单优先级更高
        //srb.addInitParameter("deny", "192.168.25.123");
        //添加控制台管理用户
        srb.addInitParameter("loginUsername", "admin");
        srb.addInitParameter("loginPassword", "123456");
        // 是否能够重置数据
        srb.addInitParameter("resetEnable", "false");
        return srb;
    }

    /**
     * 配置服务过滤器
     *
     * @return 返回过滤器配置对象
     */
    @Bean
    public FilterRegistrationBean statFilterDemo() {
        FilterRegistrationBean frb = new FilterRegistrationBean(new WebStatFilter());
        // 添加过滤规则
        frb.addUrlPatterns("/*");
        // 忽略过滤格式
        frb.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*,");
        return frb;
    }
}

